import storage from '@/utils/storage.js'
export default {
	namespaced: true, // 开启命名空间
	state: {
		userinfo: storage('userinfo').get(),
		token: storage('token').get(),
		// 登录状态
		loginStatus:false
	},
	actions: {},
	mutations: {
		// 初始化登录状态
		initUser(state){
			let userInfo = uni.getStorageSync('userinfo');
			if (userInfo) {
				state.userinfo = userInfo
				state.token = userInfo.token
				state.loginStatus = true
			}
		},
		setNickName(state, nickname) {
			state.userinfo.nickname = nickname
			storage('userinfo').set(state.userinfo)
		},
		setAvatar(state, avatar) {
			state.userinfo.avatar = avatar
			storage('userinfo').set(state.userinfo)
		},
		setUserInfo(state, userinfo) {
			state.userinfo = userinfo
			state.loginStatus = true
			storage('userinfo').set(userinfo)
		},
		setToken(state, token) {
			state.token = token
			storage('token').set(token)
		}
	},
	getters: {
		userInfo: state => state.userinfo
	},
};
